const {
  mysql
} = require('../qcloud')
const axios = require('axios');
const md5 = require('md5');
const qs = require('qs');
module.exports = async ctx => {
  const {
    open_id, //用户openID
    tel_number, //手机号
    order_type, //流量或话费
    order_info, //支支过程中返回的订单信息
    order_id, //订单号
    goods_val, //商品名称
    normal_price, //售价
    sell_price, //原价
    profit //利益
  } = ctx.request.body

  /* let sign1 = "JH71c2c82294299eb8c6d01d0a2a898301" +
    "e36637c708e516dab0587cac1090a23e" +
    tel_number.toString() +
    goods_val.split('元')[0].toString() +
    order_id.toString()
  let chargeURL =
    `https://op.juhe.cn/ofpay/mobile/onlineorder`,
    chargeData = {
      phoneno: tel_number.toString(),
      cardnum: goods_val.split('元')[0].toString(),
      orderid: order_id.toString(),
      key: "e36637c708e516dab0587cac1090a23e",
      sign: md5(sign1).toUpperCase()
    };
  try {
    axios.post(chargeURL, qs.stringify(chargeData)).then(res => {
      console.log(res.data);
    })
  } catch (error) {
    var mail = {
      // 发件人
      from: config.auth.user,
      // 主题
      subject: '话费充值失败了',
      // 收件人
      to: '431161520@qq.com',
      // 邮件内容，HTML格式
      text: `该用户的手机号为${tel_number},订单号为:${order_id},openid为：${open_id}` //可以是链接，也可以是验证码
    };
    send(mail);
  }
 */

  try {
    await mysql('phoneFlowOrder').insert({
      open_id,
      tel_number,
      order_type,
      order_info,
      order_id,
      goods_val,
      normal_price,
      sell_price,
      profit
    })
  } catch (e) {
    console.log(e);
  }

  ctx.state.data = {
    msg: 'success'
  }
}
// console.log(ctx.request.body);



const nodemailer = require('nodemailer');
var config = {
  host: 'smtp.126.com', //网易163邮箱 smtp.163.com
  port: 25, //网易邮箱端口 25
  auth: {
    user: 'yusup64@126.com', //邮箱账号
    pass: 'Yusup0604' //邮箱的授权码
  }
};
var transporter = nodemailer.createTransport(config);

function send(mail) {
  transporter.sendMail(mail, function (error, info) {
    if (error) {
      return console.log(error);
    }
    console.log('mail sent:', info.response);
  });
};

// 创建一个邮件对象
